package com.tyl.forkjoin;

import lombok.SneakyThrows;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class Test {
    public static void main(String[] args) {
        //test1();//时间1722
        test2();//1915
        //test3();//1915
    }
    //最普通计算
    public static void test1(){
        Long sum=0L;
        long start=System.currentTimeMillis();
        for (Long i = 1L; i < 100_0000_00L; i++) {
            sum+=i;
        }
        long end =System.currentTimeMillis();
        System.out.println("sum="+sum+"时间"+(end-start));
    }
    //使用ForkJoin
    @SneakyThrows
    public static void test2(){
        long start=System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> task= new ForkJoinDemo(1L,100_0000_0000L);
        ForkJoinTask<Long> submit = forkJoinPool.submit(task);
        Long sum = submit.get();
        long end =System.currentTimeMillis();
        System.out.println("sum="+sum+"时间"+(end-start));

    }
    //使用Stream计算
    public static void test3(){
        long start=System.currentTimeMillis();
        long sum = LongStream.rangeClosed(1L, 100_0000_00L).parallel().reduce(0,Long::sum);
        long end =System.currentTimeMillis();
        System.out.println("sum="+sum+"时间"+(end-start));
    }
}
